package com.library.lockProvider.service.impl;

import com.library.lockProvider.mapper.BoxAndCabinetMapper;
import com.library.lockProvider.mapper.CabinetMapper;
import com.library.lockProvider.pojo.Cabinet.Cabinet;
import com.library.lockProvider.pojo.boxAndCabinet.BoxAndCabinet;
import com.library.lockProvider.pojo.boxAndCabinet.BoxAndCabinetResult;
import com.library.lockProvider.service.BoxAndCabinetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class BoxAndCabinetImpl implements BoxAndCabinetService {

    @Autowired
    BoxAndCabinetMapper boxAndCabinetMapper;

    @Autowired
    CabinetMapper cabinetMapper;

    @Override
    @Transactional
    public BoxAndCabinetResult findBoxByS_id(String s_id) {
        BoxAndCabinetResult ans=new BoxAndCabinetResult();
        BoxAndCabinet boxAndCabinet = boxAndCabinetMapper.findBoxByS_id(s_id);
        if(boxAndCabinet!=null) {
            Cabinet cabinet=cabinetMapper.findCabinetById(boxAndCabinet.getCabinetId());
            ans.setCabinetId(boxAndCabinet.getCabinetId());
            ans.setBoxNo(boxAndCabinet.getBoxNo());
            ans.setS_id(boxAndCabinet.getS_id());
            ans.setLocation(cabinet.getLocation());
        }else{
            ans.setCabinetId(-1);
        }
        return ans;
    }

    @Override
    @Transactional
    public int appointBoxBySpare(String s_id) {

        if(boxAndCabinetMapper.findBoxByS_id(s_id)!=null || s_id==null || s_id.equals("")){
            return 0;
        }else {
            return boxAndCabinetMapper.appointBoxBySpare(s_id);
        }
    }

    @Override
    public int cancelBox(String s_id) {
        return boxAndCabinetMapper.cancelBox(s_id);
    }

    @Override
    public int initAllBox() {
        return boxAndCabinetMapper.initAllBox();
    }


}
